<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ImageRex系统</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f7f7f7;
            color: #333;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 30px;
            background-color: #fff;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            border-bottom: 2px solid #ddd;
            padding-bottom: 15px;
        }

        .header h1 {
            margin: 0;
            font-size: 2.5em;
            color: #007bff;
            font-weight: bold;
        }

        .header .actions button {
            padding: 10px 20px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 1em;
            transition: background-color 0.3s;
        }

        .header .actions button:hover {
            background-color: #0056b3;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-size: 1.1em;
            color: #555;
        }

        .form-group input, .form-group button {
            width: 100%;
            padding: 12px;
            box-sizing: border-box;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 1em;
        }

        .form-group input:focus, .form-group button:focus {
            border-color: #007bff;
            outline: none;
        }

        button {
            padding: 10px 20px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 1em;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #0056b3;
        }

        .image-list {
            list-style: none;
            padding: 0;
            margin-bottom: 30px;
        }

        .image-list li {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 15px;
            border-bottom: 1px solid #eee;
            transition: background-color 0.3s;
        }

        .image-list li:hover {
            background-color: #f9f9f9;
        }

        .image-list img {
            width: 120px;
            height: 120px;
            object-fit: cover;
            border-radius: 5px;
            margin-right: 15px;
        }

        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 30px;
        }

        .pagination button {
            padding: 8px 15px;
            margin: 0 5px;
            cursor: pointer;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: #fff;
            color: #333;
            font-size: 1em;
            transition: background-color 0.3s, color 0.3s, border-color 0.3s;
        }

        .pagination button:hover {
            background-color: #007bff;
            color: #fff;
            border-color: #007bff;
        }

        .pagination button.disabled {
            cursor: not-allowed;
            opacity: 0.5;
        }

        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.5);
            backdrop-filter: blur(5px);
            padding-top: 60px;
        }

        .modal-content {
            background-color: #fff;
            margin: 5% auto;
            padding: 30px;
            border: 1px solid #888;
            border-radius: 8px;
            width: 80%;
            max-width: 600px;
        }

        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }

        .close:hover,
        .close:focus {
            color: black;
            text-decoration: none;
            cursor: pointer;
        }

        .alert {
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid transparent;
            border-radius: 4px;
        }

        .alert-success {
            color: #3c763d;
            background-color: #dff0d8;
            border-color: #d6e9c6;
        }

        .alert-danger {
            color: #a94442;
            background-color: #f2dede;
            border-color: #ebccd1;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>ImageRex以图搜图</h1>
        <div class="actions">
            <button onclick="openUploadModal('single')">上传单张图片</button>
            <button onclick="openUploadModal('zip')">上传zip文件</button>
            <button onclick="openSearchModal()">搜索相似图片</button>
        </div>
    </div>

    <!-- 查询条件 -->
    <div class="form-group">
        <label for="search-name">图片名称</label>
        <input type="text" id="search-name" placeholder="输入图片名称">
    </div>
    <div class="form-group">
        <label for="pageIndex">页码</label>
        <input type="number" id="pageIndex" value="1">
    </div>
    <div class="form-group">
        <label for="pageSize">每页大小</label>
        <input type="number" id="pageSize" value="10">
    </div>
    <button onclick="fetchImages()">查询</button>

    <!-- 图片列表 -->
    <ul id="image-list-items" class="image-list"></ul>

    <!-- 分页导航 -->
    <div class="pagination">
        <button onclick="fetchImages(1)" id="first-page" class="disabled">首页</button>
        <button onclick="fetchImages(currentPage - 1)" id="prev-page" class="disabled">上一页</button>
        <span id="page-info"></span>
        <button onclick="fetchImages(currentPage + 1)" id="next-page" class="disabled">下一页</button>
        <button onclick="fetchImages(totalPages)" id="last-page" class="disabled">末页</button>
    </div>
</div>

<!-- 上传单张图片模态框 -->
<div id="upload-single-modal" class="modal">
    <div class="modal-content">
        <span class="close" onclick="closeUploadModal('single')">&times;</span>
        <h2>上传单张图片</h2>
        <div class="form-group">
            <label for="file-single-modal">选择图片文件</label>
            <input type="file" id="file-single-modal" accept="image/*">
        </div>
        <button onclick="uploadSingleImage('single')">上传</button>
        <div id="message-single-modal" class="alert"></div>
    </div>
</div>

<!-- 上传zip文件模态框 -->
<div id="upload-zip-modal" class="modal">
    <div class="modal-content">
        <span class="close" onclick="closeUploadModal('zip')">&times;</span>
        <h2>上传zip文件</h2>
        <div class="form-group">
            <label for="file-zip-modal">选择zip文件</label>
            <input type="file" id="file-zip-modal" accept=".zip">
        </div>
        <button onclick="uploadZipFile('zip')">上传</button>
        <div id="message-zip-modal" class="alert"></div>
    </div>
</div>

<!-- 搜索相似图片模态框 -->
<div id="search-modal" class="modal">
    <div class="modal-content">
        <span class="close" onclick="closeSearchModal()">&times;</span>
        <h2>搜索相似图片</h2>
        <div class="form-group">
            <label for="file-search-modal">选择图片文件</label>
            <input type="file" id="file-search-modal" accept="image/*">
        </div>
        <div class="form-group">
            <label for="topK-modal">返回结果数量</label>
            <input type="number" id="topK-modal" value="5">
        </div>
        <button onclick="searchImages('search')">搜索</button>
        <div id="message-search-modal" class="alert"></div>
        <ul id="results-modal" class="image-list"></ul>
    </div>
</div>

<script>
        const apiBaseUrl = '/api/image';
        let currentPage = 1;
        let totalPages = 1;

        // 打开上传模态框
        function openUploadModal(type) {
            document.getElementById(`upload-${type}-modal`).style.display = 'block';
        }

        // 关闭上传模态框
        function closeUploadModal(type) {
            document.getElementById(`upload-${type}-modal`).style.display = 'none';
            document.getElementById(`message-${type}-modal`).className = 'alert';
            document.getElementById(`message-${type}-modal`).textContent = '';
        }

        // 打开搜索相似图片模态框
        function openSearchModal() {
            document.getElementById('search-modal').style.display = 'block';
        }

        // 关闭搜索相似图片模态框
        function closeSearchModal() {
            document.getElementById('search-modal').style.display = 'none';
            document.getElementById('message-search-modal').className = 'alert';
            document.getElementById('message-search-modal').textContent = '';
            document.getElementById('results-modal').innerHTML = '';
        }

        // 上传单张图片
        function uploadSingleImage(type) {
        const fileInput = document.getElementById(`file-${type}-modal`);
        const file = fileInput.files[0];
        const messageDiv = document.getElementById(`message-${type}-modal`);

        if (!file) {
            messageDiv.className = 'alert alert-danger';
            messageDiv.textContent = '请选择一个文件';
            return;
        }

        const formData = new FormData();
        formData.append('file', file);

        fetch(`${apiBaseUrl}/upload/single`, {
            method: 'POST',
            body: formData
        })
        .then(response => response.text())
        .then(data => {
            messageDiv.className = 'alert alert-success';
            messageDiv.textContent = data;
            closeUploadModal(type);
            fetchImages();
        })
        .catch(error => {
            messageDiv.className = 'alert alert-danger';
            messageDiv.textContent = error.message;
        });
    }

    // 上传zip文件
    function uploadZipFile(type) {
        const fileInput = document.getElementById(`file-${type}-modal`);
        const file = fileInput.files[0];
        const messageDiv = document.getElementById(`message-${type}-modal`);

        if (!file) {
            messageDiv.className = 'alert alert-danger';
            messageDiv.textContent = '请选择一个文件';
            return;
        }

        const formData = new FormData();
        formData.append('file', file);

        fetch(`${apiBaseUrl}/upload/zip`, {
            method: 'POST',
            body: formData
        })
        .then(response => response.text())
        .then(data => {
            messageDiv.className = 'alert alert-success';
            messageDiv.textContent = data;
            closeUploadModal(type);
            fetchImages();
        })
        .catch(error => {
            messageDiv.className = 'alert alert-danger';
            messageDiv.textContent = error.message;
        });
    }

    // 搜索相似图片
    function searchImages(type) {
        const fileInput = document.getElementById(`file-${type}-modal`);
        const file = fileInput.files[0];
        const topKInput = document.getElementById('topK-modal');
        const topK = topKInput.value;
        const messageDiv = document.getElementById(`message-${type}-modal`);
        const resultsList = document.getElementById('results-modal');

        if (!file) {
            messageDiv.className = 'alert alert-danger';
            messageDiv.textContent = '请选择一个文件';
            return;
        }

        const formData = new FormData();
        formData.append('file', file);
        formData.append('topK', topK);

        fetch(`${apiBaseUrl}/search`, {
            method: 'POST',
            body: formData
        })
        .then(response => response.json())
        .then(data => {
            messageDiv.className = 'alert alert-success';
            messageDiv.textContent = '搜索成功';
            resultsList.innerHTML = '';
            data.forEach(item => {
                const li = document.createElement('li');
                const img = document.createElement('img');
                img.src = `data:image/jpeg;base64,${item.entity.image}`;
                img.alt = item.entity.name;
                const scoreSpan = document.createElement('span');
                scoreSpan.textContent = `相似评分: ${item.score.toFixed(2)}`;
                li.appendChild(img);
                li.appendChild(document.createTextNode(item.entity.name));
                li.appendChild(scoreSpan);
                resultsList.appendChild(li);
            });
        })
        .catch(error => {
            messageDiv.className = 'alert alert-danger';
            messageDiv.textContent = error.message;
        });
    }

    // 获取图片列表
    function fetchImages(page = 1) {
        const nameInput = document.getElementById('search-name');
        const pageIndexInput = document.getElementById('pageIndex');
        const pageSizeInput = document.getElementById('pageSize');
        const name = nameInput.value;
        const pageIndex = page;
        const pageSize = parseInt(pageSizeInput.value);
        const imageListItems = document.getElementById('image-list-items');
        const pageInfo = document.getElementById('page-info');
        const firstPageBtn = document.getElementById('first-page');
        const prevPageBtn = document.getElementById('prev-page');
        const nextPageBtn = document.getElementById('next-page');
        const lastPageBtn = document.getElementById('last-page');

        const req = {
            name,
            pageIndex,
            pageSize
        };

        fetch(`${apiBaseUrl}/page`, {
            method: 'POST',
            body: JSON.stringify(req),
            headers: {
                'Content-Type': 'application/json'
            }
        })
        .then(response => response.json())
        .then(data => {
            imageListItems.innerHTML = '';
            data.data.forEach(image => {
                const li = document.createElement('li');
                const img = document.createElement('img');
                img.src = `data:image/jpeg;base64,${image.image}`;
                img.alt = image.name;
                const deleteButton = document.createElement('button');
                deleteButton.textContent = '删除';
                deleteButton.onclick = () => deleteImage(image.name);
                li.appendChild(img);
                li.appendChild(document.createTextNode(image.name));
                li.appendChild(deleteButton);
                imageListItems.appendChild(li);
            });

            currentPage = data.pageIndex;
            totalPages = data.totalPages;

            pageInfo.textContent = `第 ${currentPage} 页 / 共 ${totalPages} 页`;

            firstPageBtn.disabled = currentPage === 1;
            prevPageBtn.disabled = currentPage === 1;
            nextPageBtn.disabled = currentPage === totalPages;
            lastPageBtn.disabled = currentPage === totalPages;

            pageIndexInput.value = currentPage;
        })
        .catch(error => {
            alert(error.message);
        });
    }

    // 删除图片
    function deleteImage(name) {

        const req = {
            name
        };
        fetch(`${apiBaseUrl}/delete`, {
            method: 'POST',
            body: JSON.stringify(req),
            headers: {
                'Content-Type': 'application/json'
            }
        })
        .then(response => response.text())
        .then(data => {
            alert(data);
            fetchImages();
        })
        .catch(error => {
            alert(error.message);
        });
    }

    // 初始化
    fetchImages();
</script>
</body>
</html>